package com.lzh.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * @author zhehen.lu
 * @date 2025/8/3 14:14
 */
public class EasyExcelDoubleRound2BitPercentConverter implements Converter<Double> {
    public EasyExcelDoubleRound2BitPercentConverter() {
    }

    @Override
    public Class supportJavaTypeKey() {
        return Double.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<String> convertToExcelData(WriteConverterContext<Double> context) throws Exception {
        Double value = (Double)context.getValue();
        if (value == null) {
            return new WriteCellData("0%");
        } else {
            double v = (double)Math.round(value * 10000.0D) / 100.0D;
            return new WriteCellData(Double.toString(v) + "%");
        }
    }
}